import ls from '@/lib/ls'
/**
 * 项目API错误
 */
export class ApiError extends Error {
  constructor(code = 0, ...params) {
    super(...params)
    if (typeof Error.captureStackTrace === 'function') {
      Error.captureStackTrace(this, ApiError);
    }
    this.name = 'ApiError'
    this.code = code
    this.date = new Date()
  }

  solve() {
    const { code } = this
    this.prompt()
    // token鉴权失败
    if (code === 401) { 
      setTimeout(function () {
        ls.remove('TOKEN')
        let envpath  = import.meta.env.VITE_PATH
        if(envpath){
          window.location.href = '/' + envpath + '/login' 
        }else{
          window.location.href = '/login' 
        }
      }, 1300)
    }
  }

  prompt() {
    const { message } = this
    console.log("错误信息", message)
  }
}
